#ifndef _WAVES_H_
#define _WAVES_H_

#include "d3dUtil.h"
#include "Effects.h"
#include "TextureMgr.h"



class Waves
{
private:
	DWORD mNumRows;
	DWORD mNumCols;

	DWORD mNumVertices;
	DWORD mNumIndices;

	ID3D10Device	*md3dDevice;

	ID3D10Buffer				*mVertexBuffer;
	ID3D10Buffer				*mIndexBuffer;
	ID3D10BlendState			*mBlendState;
	ID3D10ShaderResourceView	*mDiffuseMap;
	ID3D10ShaderResourceView	*mSpecMap;

	D3DXMATRIX mWorldMatrix;
	D3DXMATRIX mTexMatrix;

	float mStepTime;
	float mStepSpace;
	float mK;
	D3DXVECTOR3 *mPrevSolution;
	D3DXVECTOR3 *mCurrSolution;
	D3DXVECTOR3 *mNormals;

	// disturbans frequency
	float mDisturbance;

	// wave tex translation
	D3DXVECTOR2 mTexTranslation;
	float mTransSpeed;
	float mTransAmp;
	float mTransRot;

	float mDx;
	float mHalfDepth;
	float mHalfWidth;

public:
	Waves() : mNumRows(0), mNumCols(0),  mNumVertices(0), mNumIndices(0),
		md3dDevice(0), 
		mVertexBuffer(0), mIndexBuffer(0), mBlendState(0), mDiffuseMap(0), mSpecMap(0),
		mStepTime(0), mStepSpace(0), mK(0), 
		mPrevSolution(0), mCurrSolution(0), mNormals(0), 
		mDisturbance(0.5f), 
		mTexTranslation(0.0f, 0.0f), mTransSpeed(0.1f), mTransAmp(0.25f), mTransRot(4.0f)
	{
		D3DXMatrixIdentity(&mWorldMatrix);
		D3DXMatrixIdentity(&mTexMatrix);
	}
	virtual ~Waves() {
		ReleaseCOM(mVertexBuffer);
		ReleaseCOM(mIndexBuffer);
		ReleaseCOM(mBlendState);

		delete[] mPrevSolution;
		delete[] mCurrSolution;
		delete[] mNormals;
	}

	void init(ID3D10Device *device, DWORD m, DWORD n, float dx, float dt, float speed);
	void update(float dt);
	void draw();
	
	D3DXMATRIX	getWorldMatrix();
	D3DXMATRIX	getTexMatrix();
	ID3D10ShaderResourceView* getDiffuseMap(ID3D10ShaderResourceView* defaultMap);
	ID3D10ShaderResourceView* getSpecMap(ID3D10ShaderResourceView* defaultMap);

	void pick(float x, float z);

private:
	void buildBuffers(ID3D10Device *device, DWORD m, DWORD n, float dx, float dt, float speed);
	void buildResourceViews();
	void buildStates();
	void disturb(DWORD i, DWORD j, float magnitude);
};



#endif//_WAVES_H_
